<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools>
        <!-- 右边的按钮 -->
        <el-button slot="after" type="primary" size="small" @click="addPermission(1,0)">添加权限</el-button>
      </page-tools>
      <!-- 表格 指定id为唯一标识 -->
      <el-table border :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <!-- 添加按钮只在type == 1时显示 -->
            <el-button v-if="row.type == 1" type="text" :disabled="!checkPermission('permission')" @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="deletePermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增编辑弹层 -->
    <el-dialog :visible="showDialog" :title="showText" @close="concle">
      <!-- 表单 -->
      <el-form ref="permForm" label-width="120px" :model="fromData" :rules="rules">
        <el-form-item label="名称" style="width:90%" prop="name">
          <el-input v-model="fromData.name" />
        </el-form-item>
        <el-form-item label="标识" style="width:90%" prop="code">
          <el-input v-model="fromData.code" />
        </el-form-item>
        <el-form-item label="描述" style="width:90%">
          <el-input v-model="fromData.description" />
        </el-form-item>
        <el-form-item label="开关" style="width:90%">
          <!-- 值为1时激活 0时不激活 -->
          <el-switch v-model="fromData.enVisible" inactive-value="0" active-value="1" />
        </el-form-item>
      </el-form>
      <!-- 底部确定 -->
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="concle">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确认</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, updatePermission, getPermissionDetail } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [],
      showDialog: false,
      fromData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '1 ' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.fromData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    // 获取权限
    this.getPermissionList()
  },
  methods: {
    // 编辑
    async editPermission(id) {
      this.fromData = await getPermissionDetail(id)
      this.showDialog = true
    },
    // 点击确认
    async btnOK() {
      try {
        await this.$refs.permForm.validate()
        if (this.fromData.id) {
          // 调用编辑接口
          await updatePermission(this.fromData)
        } else {
          // 调用新增接口
          await addPermission(this.fromData)
        }
        this.$message.success('操作成功')
        this.showDialog = false
        this.getPermissionList()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取权限
    async getPermissionList() {
      // 将数据转化成带children的结构
      this.list = tranListToTreeData(await getPermissionList(), '0')
    },
    // 弹出层关闭
    concle() {
      this.fromData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '1 ' // 开启
      }
      this.$refs.permForm.clearValidate()
      // 还原数据 移除校验
      this.showDialog = false
    },
    // 点击删除
    deletePermission(id) {
      this.$confirm('确认删除该权限吗？').then(async() => {
        return delPermission(id)
      }).then(() => {
        this.$message.success('删除成功')
        this.getPermissionList()
      })
    },
    addPermission(type, pid) {
      // type类型 pid父节点的标识
      this.fromData.type = type
      this.fromData.pid = pid
      this.showDialog = true
    }
  }
}
</script>

<style>

</style>
